package com.crape.topic.bo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crape.common.exceptions.BOException;
import com.crape.common.model.SubjectRef;
import com.crape.topic.bo.ISubjectRefBO;
import com.crape.topic.dao.ISubjectDAO;
import com.crape.topic.dao.ISubjectRefDAO;

@Service
public class SubjectRefBOImpl implements ISubjectRefBO {

	private ISubjectRefDAO subjectRefDAOImpl;

	private ISubjectDAO subjectDAOImpl;

	@Autowired
	public void setsubjectRefDAOImpl(ISubjectRefDAO subjectRefDAOImpl) {
		this.subjectRefDAOImpl = subjectRefDAOImpl;
	}

	@Autowired
	public void setsubjectDAOImpl(ISubjectDAO subjectDAOImpl) {
		this.subjectDAOImpl = subjectDAOImpl;
	}

	@Override
	public Integer countSubjectAttention(Long subjectid) throws BOException {
		if (subjectid == null) {
			throw new IllegalArgumentException("subjectid is null");
		}
		return subjectRefDAOImpl.countSubjectRefs(subjectid);
	}

	@Override
	public Integer countSubjectAttention(Long accountid, Long subjectid) throws BOException {
		if (accountid == null || subjectid == null) {
			throw new IllegalArgumentException("accountid||subjectid is null");
		}
		SubjectRef SubjectRef = new SubjectRef();
		SubjectRef.setAccountid(accountid);
		SubjectRef.setSubjectid(subjectid);
		return subjectRefDAOImpl.countSubjectRefs(SubjectRef);
	}

	@Override
	public void addSubjectAttention(Long accountid, Long subjectid) throws BOException {
		if (accountid == null || subjectid == null) {
			throw new IllegalArgumentException("accountid||subjectid is null");
		}
		SubjectRef SubjectRef = new SubjectRef();
		SubjectRef.setAccountid(accountid);
		SubjectRef.setSubjectid(subjectid);
		Integer num = subjectRefDAOImpl.countSubjectRefs(SubjectRef);
		if (num == null || num == 0) {
			subjectRefDAOImpl.insertSubjectRef(SubjectRef);
		}
		subjectDAOImpl.updateSubjectAttentionCount(subjectid);
	}

	@Override
	public void cancelSubjectAttention(Long accountid, Long subjectid) throws BOException {
		if (accountid == null || subjectid == null) {
			throw new IllegalArgumentException("accountid||subjectid is null");
		}
		SubjectRef SubjectRef = new SubjectRef();
		SubjectRef.setAccountid(accountid);
		SubjectRef.setSubjectid(subjectid);
		subjectRefDAOImpl.backupSubjectRef(SubjectRef);
		subjectRefDAOImpl.deleteSubjectRef(SubjectRef);
		subjectDAOImpl.updateSubjectAttentionCount(subjectid);
	}

}
